﻿using kad2SimSoft.Classes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für capacityPlanWindow.xaml
    /// </summary>
    public partial class CapacityPlanControl : UserControl
    {

        //private List<Endproduct> _endproducts;
        //private List<ManufacturedPart> _manufacturedParts;
        private List<Workplace> _workplaces;
        private List<Waitinglist> _waitinglistStock;
        private List<Order> _orders;

        private ArrayList _addition;

        public List<Workplace> Workplaces
        {
            get { return _workplaces; }
            set { _workplaces = value; }
        }

        public CapacityPlanControl(MainWindow main)
        {
            InitializeComponent();
            _workplaces = main.Workplaces;
            _waitinglistStock = main.WaitinglistStock;
            _orders = main.ProductionOrderView.getSortedOrderList();
            saveAddition();
            clearTable();
            calculateCapacity();
            fillTable();
        }


        //public void initTestData()
        //{
        //_endproducts = FindPageControl(this).Endproducts;
        //_manufacturedParts = FindPageControl(this).ManufacturedParts;
        //    Order order = null;
        //    int i = 1;
        //    foreach (Endproduct product in _endproducts)
        //    {
        //        order = new Order();
        //        if (product.Amount == 0)
        //        {
        //            order.Amount = product.Amount * 2;
        //        }
        //        else
        //        {
        //            order.Amount = 300;
        //        }
        //        order.Id = i;
        //        order.Product = product;
        //        i++;
        //        _orders.Add(order);
        //    }

        //    foreach (ManufacturedPart product in _manufacturedParts)
        //    {
        //        order = new Order();
        //        if (product.Amount == 0)
        //        {
        //            order.Amount = product.Amount * 3;
        //        }
        //        else
        //        {
        //            order.Amount = 500;
        //        }
        //        order.Id = i;
        //        order.Product = product;
        //        i++;
        //        _orders.Add(order);
        //    }
        //}




        public void calculateCapacity()
        {

            //_workplaces = FindPageControl(this).Workplaces;
            //_waitinglistStock = FindPageControl(this).WaitinglistStock;
            //_orders = FindPageControl(this).ProducionPlanningView.CreateListOforders();
            WorkplaceCapacity capacity;



            foreach (Workplace place in _workplaces)
            {

                int amountSetupTime = 0;
                int amountProductionTime = 0;
                int totalTime = 0;
                int addition = 0;
                double additionValue = 0;
                double subTotal = 0;
                double totalTimeWithAddition = 0;
                double overtime = 0;
                int desposit = 0;

                foreach (SettingUpTime time in place.SettingUpTime)
                {

                    foreach (WaitinglistStock waitingList in _waitinglistStock)
                    {

                        if (waitingList.Item == time.PartId)
                        {
                            amountProductionTime = amountProductionTime + waitingList.Amount * time.ProductionTime;
                            amountSetupTime = amountSetupTime + time.SetupTime;
                        }
                    }

                    foreach (Order order in _orders)
                    {

                            if (order.Product.Id == time.PartId)
                            {
                                //Rückstände (Warteliste) aus der Vorperiode addieren


                                if (order.Amount > 0)
                                {
                                amountProductionTime = amountProductionTime + order.Amount * time.ProductionTime;
                                amountSetupTime = amountSetupTime + time.SetupTime;
                                }
                            }
                    }

                }

                //Kapazitäten berechnen (inkl. Aufschlag)
                totalTime = amountProductionTime + amountSetupTime;
                //addition = Convert.ToInt32(tbAddition1.Text);
                #region Aufschläge ermitteln
                switch (place.Id)
                {
                    case 1:
                        if (_addition[0].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[0].ToString());
                        }
                        break;
                    case 2:
                        if (_addition[1].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[1].ToString());
                        }
                        break;
                    case 3:
                        if (_addition[2].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[2].ToString());
                        }
                        break;
                    case 4:
                        if (_addition[3].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[3].ToString());
                        }
                        break;
                    case 6:
                        if (_addition[4].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[4].ToString());
                        }
                        break;
                    case 7:
                        if (_addition[5].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[5].ToString());
                        }
                        break;
                    case 8:
                        if (_addition[6].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[6].ToString());
                        }
                        break;
                    case 9:
                        if (_addition[7].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[7].ToString());
                        }
                        break;
                    case 10:
                        if (_addition[8].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[8].ToString());
                        }
                        break;
                    case 11:
                        if (_addition[9].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[9].ToString());
                        }
                        break;
                    case 12:
                        if (_addition[10].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[10].ToString());
                        }
                        break;
                    case 13:
                        if (_addition[11].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[11].ToString());
                        }
                        break;
                    case 14:
                        if (_addition[12].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[12].ToString());
                        }
                        break;
                    case 15:
                        if (_addition[13].ToString().Equals("") == false)
                        {
                            addition = Convert.ToInt32(_addition[13].ToString());
                        }
                        break;
                }
                #endregion

                //Überprüfen, ob der Aufschlag 0 ist
                if (addition == 0)
                {
                    additionValue = 1;
                }
                else
                {
                    subTotal = (double)addition / 100.0;
                    additionValue = 1 + subTotal;
                }


                totalTimeWithAddition = totalTime * additionValue;
                //totalTimeWithAddition = (int)totalTimeWithAddition;
                totalTimeWithAddition = Math.Round(totalTimeWithAddition);

                if (totalTimeWithAddition > 7200) {
                    totalTimeWithAddition = 7200;
                }

                if (totalTimeWithAddition < 3600) {
                    desposit = 1;
                    overtime = 0;
                    if (totalTimeWithAddition > 2400) { 
                    overtime = totalTimeWithAddition - 2400;
                    }
                }
                else if (totalTimeWithAddition >= 3600 && totalTimeWithAddition < 6000)
                {
                    desposit = 2;
                    overtime = 0;
                    if (totalTimeWithAddition > 4800)
                    {
                        overtime = totalTimeWithAddition - 4800;
                    }

                }
                else {
                    desposit = 3;
                    overtime = 0;
                }

                ////Überstunden berechnen
                //if (totalTimeWithAddition > 3600 && totalTimeWithAddition < 4800)
                //{
                //    overtime = 0;
                //}
                //else if (totalTimeWithAddition < 2401)
                //{
                //    overtime = 0;
                //}
                //else if (totalTimeWithAddition >= 6000)
                //{
                //    overtime = 0;
                //}

                ////Schichten berechnen
                //if (totalTimeWithAddition > 4800)
                //{
                //    desposit = 3;
                //}
                //else if (totalTimeWithAddition > 3600)
                //{
                //    desposit = 2;
                //}
                //else
                //{
                //    desposit = 1;
                //}

                capacity = new WorkplaceCapacity(amountSetupTime, amountProductionTime, totalTime, addition, additionValue, totalTimeWithAddition, overtime, desposit);
                place.Capacity = capacity;

            }
        }

        private void refreshWorkplaceCapacity(int workplaceId)
        {

            int productionTime = 0;
            int setupTime = 0;
            int totalTime = 0;
            int addition = 0;
            double additionValue = 0;
            double subTotal = 0;
            double totalTimeWithAddition = 0;
            double overtime = 0;
            int desposit = 0;

            #region getValues
            switch (workplaceId)
            {
                case 1:
                    productionTime = Convert.ToInt32(tbProdTime1.Text);
                    setupTime = Convert.ToInt32(tbSetupTime1.Text);
                    totalTime = Convert.ToInt32(tbTotalTime1.Text);
                    //if (tbAddition1.Text == "")
                    //{
                    //    tbAddition1.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition1.Text);
                    //}
                    break;
                case 2:
                    productionTime = Convert.ToInt32(tbProdTime2.Text);
                    setupTime = Convert.ToInt32(tbSetupTime2.Text);
                    totalTime = Convert.ToInt32(tbTotalTime2.Text);

                    //if (tbAddition2.Text == "")
                    //{
                    //    tbAddition2.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition2.Text);
                    //}

                    
                    break;
                case 3:
                    productionTime = Convert.ToInt32(tbProdTime3.Text);
                    setupTime = Convert.ToInt32(tbSetupTime3.Text);
                    totalTime = Convert.ToInt32(tbTotalTime3.Text);

                    //if (tbAddition3.Text == "")
                    //{
                    //    tbAddition3.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition3.Text);
                    //}

                    
                    break;
                case 4:
                    productionTime = Convert.ToInt32(tbProdTime4.Text);
                    setupTime = Convert.ToInt32(tbSetupTime4.Text);
                    totalTime = Convert.ToInt32(tbTotalTime4.Text);

                    //if (tbAddition4.Text == "")
                    //{
                    //    tbAddition4.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition4.Text);
                    //}

                    break;
                case 6:
                    productionTime = Convert.ToInt32(tbProdTime6.Text);
                    setupTime = Convert.ToInt32(tbSetupTime6.Text);
                    totalTime = Convert.ToInt32(tbTotalTime6.Text);

                    //if (tbAddition6.Text == "")
                    //{
                    //    tbAddition6.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition6.Text);
                    //}

                    
                    break;
                case 7:
                    productionTime = Convert.ToInt32(tbProdTime7.Text);
                    setupTime = Convert.ToInt32(tbSetupTime7.Text);
                    totalTime = Convert.ToInt32(tbTotalTime7.Text);

                    //if (tbAddition7.Text == "")
                    //{
                    //    tbAddition7.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition7.Text);
                    //}
                    break;
                case 8:
                    productionTime = Convert.ToInt32(tbProdTime8.Text);
                    setupTime = Convert.ToInt32(tbSetupTime8.Text);
                    totalTime = Convert.ToInt32(tbTotalTime8.Text);

                    //if (tbAddition8.Text == "")
                    //{
                    //    tbAddition8.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition8.Text);
                    //}

                    
                    break;
                case 9:
                    productionTime = Convert.ToInt32(tbProdTime9.Text);
                    setupTime = Convert.ToInt32(tbSetupTime9.Text);
                    totalTime = Convert.ToInt32(tbTotalTime9.Text);

                    //if (tbAddition9.Text == "")
                    //{
                    //    tbAddition9.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition9.Text);
                    //}


                    break;
                case 10:
                    productionTime = Convert.ToInt32(tbProdTime10.Text);
                    setupTime = Convert.ToInt32(tbSetupTime10.Text);
                    totalTime = Convert.ToInt32(tbTotalTime10.Text);

                    //if (tbAddition10.Text == "")
                    //{
                    //    tbAddition10.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition10.Text);
                    //}       
                    break;
                case 11:
                    productionTime = Convert.ToInt32(tbProdTime11.Text);
                    setupTime = Convert.ToInt32(tbSetupTime11.Text);
                    totalTime = Convert.ToInt32(tbTotalTime11.Text);

                    //if (tbAddition11.Text == "")
                    //{
                    //    tbAddition11.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition11.Text);
                    //}

                    
                    break;
                case 12:
                    productionTime = Convert.ToInt32(tbProdTime12.Text);
                    setupTime = Convert.ToInt32(tbSetupTime12.Text);
                    totalTime = Convert.ToInt32(tbTotalTime12.Text);

                    //if (tbAddition12.Text == "")
                    //{
                    //    tbAddition12.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition12.Text);
                    //}

                    
                    break;
                case 13:
                    productionTime = Convert.ToInt32(tbProdTime13.Text);
                    setupTime = Convert.ToInt32(tbSetupTime13.Text);
                    totalTime = Convert.ToInt32(tbTotalTime13.Text);

                    //if (tbAddition13.Text == "")
                    //{
                    //    tbAddition13.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition13.Text);
                    //}

                    
                    break;
                case 14:
                    productionTime = Convert.ToInt32(tbProdTime14.Text);
                    setupTime = Convert.ToInt32(tbSetupTime14.Text);
                    totalTime = Convert.ToInt32(tbTotalTime14.Text);

                    //if (tbAddition14.Text == "")
                    //{
                    //    tbAddition14.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition14.Text);
                    //}

                    
                    break;
                case 15:
                    productionTime = Convert.ToInt32(tbProdTime15.Text);
                    setupTime = Convert.ToInt32(tbSetupTime15.Text);
                    totalTime = Convert.ToInt32(tbTotalTime15.Text);

                    //if (tbAddition15.Text == "")
                    //{
                    //    tbAddition15.Text = "0";
                    //    addition = 0;
                    //}
                    //else
                    //{
                        addition = Convert.ToInt32(tbAddition15.Text);
                    //}

                    
                    break;
            }
#endregion

            #region Berechnung
            //Berechnung Start
            //Überprüfen, ob der Aufschlag 0 ist
            if (addition == 0)
            {
                additionValue = 1;
            }
            else
            {
                subTotal = (double)addition / 100.0;
                additionValue = 1 + subTotal;
            }

            totalTimeWithAddition = totalTime * additionValue;
            totalTimeWithAddition = Math.Round(totalTimeWithAddition);

            if (totalTimeWithAddition > 7200)
            {
                totalTimeWithAddition = 7200;
            }

            if (totalTimeWithAddition < 3600)
            {
                desposit = 1;
                overtime = 0;
                if (totalTimeWithAddition > 2400)
                {
                    overtime = totalTimeWithAddition - 2400;
                }
            }
            else if (totalTimeWithAddition >= 3600 && totalTimeWithAddition < 6000)
            {
                desposit = 2;
                overtime = 0;
                if (totalTimeWithAddition > 4800)
                {
                    overtime = totalTimeWithAddition - 4800;
                }

            }
            else
            {
                desposit = 3;
                overtime = 0;
            }



            //Überstunden berechnen
            //if (totalTimeWithAddition > 3599)
            //{
            //    overtime = 0;
            //}
            //else if (totalTimeWithAddition < 2401)
            //{
            //    overtime = 0;
            //}
            //else
            //{
            //    overtime = (totalTimeWithAddition - 2400);
            //}

            ////Schichten berechnen
            //if (totalTimeWithAddition > 4800)
            //{
            //    desposit = 3;
            //}
            //else if (totalTimeWithAddition > 3600)
            //{
            //    desposit = 2;
            //}
            //else
            //{
            //    desposit = 1;
            //}
            #endregion

            #region setValues
            switch (workplaceId)
            {
                case 1:
                    tbTotalTimeWithAdd1.Text = totalTimeWithAddition.ToString();
                    tbOvertime1.Text = overtime.ToString();
                    tbShift1.Text = desposit.ToString();
                    break;
                case 2:
                    tbTotalTimeWithAdd2.Text = totalTimeWithAddition.ToString();
                    tbOvertime2.Text = overtime.ToString();
                    tbShift2.Text = desposit.ToString();
                    break;
                case 3:
                    tbTotalTimeWithAdd3.Text = totalTimeWithAddition.ToString();
                    tbOvertime3.Text = overtime.ToString();
                    tbShift3.Text = desposit.ToString();
                    break;
                case 4:
                    tbTotalTimeWithAdd4.Text = totalTimeWithAddition.ToString();
                    tbOvertime4.Text = overtime.ToString();
                    tbShift4.Text = desposit.ToString();
                    break;
                case 6:
                    tbTotalTimeWithAdd6.Text = totalTimeWithAddition.ToString();
                    tbOvertime6.Text = overtime.ToString();
                    tbShift6.Text = desposit.ToString();
                    break;
                case 7:
                    tbTotalTimeWithAdd7.Text = totalTimeWithAddition.ToString();
                    tbOvertime7.Text = overtime.ToString();
                    tbShift7.Text = desposit.ToString();
                    break;
                case 8:
                    tbTotalTimeWithAdd8.Text = totalTimeWithAddition.ToString();
                    tbOvertime8.Text = overtime.ToString();
                    tbShift8.Text = desposit.ToString();
                    break;
                case 9:
                    tbTotalTimeWithAdd9.Text = totalTimeWithAddition.ToString();
                    tbOvertime9.Text = overtime.ToString();
                    tbShift9.Text = desposit.ToString();
                    break;
                case 10:
                    tbTotalTimeWithAdd10.Text = totalTimeWithAddition.ToString();
                    tbOvertime10.Text = overtime.ToString();
                    tbShift10.Text = desposit.ToString();
                    break;
                case 11:
                    tbTotalTimeWithAdd11.Text = totalTimeWithAddition.ToString();
                    tbOvertime11.Text = overtime.ToString();
                    tbShift11.Text = desposit.ToString();
                    break;
                case 12:
                    tbTotalTimeWithAdd12.Text = totalTimeWithAddition.ToString();
                    tbOvertime12.Text = overtime.ToString();
                    tbShift12.Text = desposit.ToString();
                    break;
                case 13:
                    tbTotalTimeWithAdd13.Text = totalTimeWithAddition.ToString();
                    tbOvertime13.Text = overtime.ToString();
                    tbShift13.Text = desposit.ToString();
                    break;
                case 14:
                    tbTotalTimeWithAdd14.Text = totalTimeWithAddition.ToString();
                    tbOvertime14.Text = overtime.ToString();
                    tbShift14.Text = desposit.ToString();
                    break;
                case 15:
                    tbTotalTimeWithAdd15.Text = totalTimeWithAddition.ToString();
                    tbOvertime15.Text = overtime.ToString();
                    tbShift15.Text = desposit.ToString();
                    break;
            }
#endregion

        }


        public void fillTable()
        {

            int zl = 0;

            foreach (Workplace place in _workplaces)
            {

                foreach (Control control in InputForm.Children)
                {

                    if (control.GetType().ToString().Equals("System.Windows.Controls.TextBox"))
                    {
                        TextBox tb = (TextBox)control;
                        if (tb.Text.Equals(""))
                        {

                            switch (zl)
                            {
                                case 0:
                                    tb.Text = place.Capacity.AmountProductionTime.ToString();
                                    break;
                                case 1:
                                    tb.Text = place.Capacity.AmountSetupTime.ToString();
                                    break;
                                case 2:
                                    tb.Text = place.Capacity.TotalTime.ToString();
                                    break;
                                case 3:
                                    tb.Text = place.Capacity.Addition.ToString();
                                    break;
                                case 4:
                                    tb.Text = place.Capacity.TotalTimeWithAddition.ToString();
                                    break;
                                case 5:
                                    tb.Text = place.Capacity.Overtime.ToString();
                                    break;
                                case 6:
                                    tb.Text = place.Capacity.Deposit.ToString();

                                    break;
                            }
                            zl = zl + 1;
                            tb.UpdateLayout();

                        }

                    }

                    if (zl > 6)
                    {
                        zl = 0;
                        break;
                    }

                }

            }

        }

        public static MainWindow FindPageControl(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);

            if (parent == null) return null;

            MainWindow page = parent as MainWindow;
            if (page != null)
            {
                return page;
            }
            else
            {
                return FindPageControl(parent);
            }
        }

        public void saveCapacity() {

            List<Capacity> capacity = new List<Capacity>();
            capacity.Add(new Capacity("1",tbOvertime1.Text,tbShift1.Text));
            capacity.Add(new Capacity("2", tbOvertime2.Text, tbShift2.Text));
            capacity.Add(new Capacity("3", tbOvertime3.Text, tbShift3.Text));
            capacity.Add(new Capacity("4", tbOvertime4.Text, tbShift4.Text));
            capacity.Add(new Capacity("6", tbOvertime6.Text, tbShift6.Text));
            capacity.Add(new Capacity("7", tbOvertime7.Text, tbShift7.Text));
            capacity.Add(new Capacity("8", tbOvertime8.Text, tbShift8.Text));
            capacity.Add(new Capacity("9", tbOvertime9.Text, tbShift9.Text));
            capacity.Add(new Capacity("10", tbOvertime10.Text, tbShift10.Text));
            capacity.Add(new Capacity("11", tbOvertime11.Text, tbShift11.Text));
            capacity.Add(new Capacity("12", tbOvertime12.Text, tbShift12.Text));
            capacity.Add(new Capacity("13", tbOvertime13.Text, tbShift13.Text));
            capacity.Add(new Capacity("14", tbOvertime14.Text, tbShift14.Text));
            capacity.Add(new Capacity("15", tbOvertime15.Text, tbShift15.Text));

            FindPageControl(this).Capacity = capacity;

        }


        private void saveAddition() {

            _addition = new ArrayList();

            foreach (Control control in InputForm.Children)
            {
                if (control.GetType().ToString().Equals("System.Windows.Controls.TextBox"))
                {
                    TextBox tb = (TextBox)control;
                    if (tb.Name.IndexOf("Addition", 0) > 0) {
                        _addition.Add(tb.Text);
                    } 
                }
            }
        }


        private void clearTable()
        {

            foreach (Control control in InputForm.Children)
            {
                if (control.GetType().ToString().Equals("System.Windows.Controls.TextBox"))
                {
                    TextBox tb = (TextBox)control;
                    tb.Text = "";
                }
            }
        }

        private static bool isValueNumeric(string text)
        {
            Regex regex = new Regex("[^0-9.]+"); //regex that matches disallowed text
            return !regex.IsMatch(text);
        }


        #region Aufschläge aktualisieren

        private void tbAddition1_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(1);
        }

        private void tbAddition2_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(2);
        }

        private void tbAddition3_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(3);
        }

        private void tbAddition4_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(4);
        }

        private void tbAddition6_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(6);
        }

        private void tbAddition7_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(7);
        }

        private void tbAddition8_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(8);
        }

        private void tbAddition9_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(9);
        }

        private void tbAddition10_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(10);
        }

        private void tbAddition11_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(11);
        }

        private void tbAddition12_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(12);
        }

        private void tbAddition13_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(13);
        }

        private void tbAddition14_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(14);
        }

        private void tbAddition15_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 100) { tb.Text = "100"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            refreshWorkplaceCapacity(15);
        }


        #endregion


        private void btn_nextToPurchasindOrders_Click(object sender, RoutedEventArgs e)
        {
            saveCapacity();
            FindPageControl(this).btCapacityPlanning.IsChecked = false;
            FindPageControl(this).btPurchasingPlanning.IsChecked = true;
            FindPageControl(this).setButtonsEnabledAfterCapacity();
            FindPageControl(this).ChangeViewTo(MainWindow.VIEWTYPE.PURCHASING);
        }

        private void btn_backToProductionOrders_Click(object sender, RoutedEventArgs e)
        {
            FindPageControl(this).btCapacityPlanning.IsChecked = false;
            FindPageControl(this).btOrderPlanning.IsChecked = true;
            FindPageControl(this).ChangeViewTo(MainWindow.VIEWTYPE.ORDER);
        }

        //Überstunden aktualisieren
        #region KeyUp Overtime
        private void tbOvertime1_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime2_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime3_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime4_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime6_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime7_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime8_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime9_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime10_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime11_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime12_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime13_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime14_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        private void tbOvertime15_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "0"; }
            if (Convert.ToInt32(tb.Text) > 1200) { tb.Text = "1200"; }
            if (Convert.ToInt32(tb.Text) < 0) { tb.Text = "0"; }
            saveCapacity();
        }

        #endregion 

        //Schichten aktualisieren
        #region KeyUp Shift
        private void tbShift1_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift2_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift3_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift4_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift6_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift7_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift8_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();

        }

        private void tbShift9_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift10_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift11_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift12_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift13_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift14_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        private void tbShift15_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = new TextBox();
            tb = (TextBox)sender;
            if (tb.Text.Equals("") || (!isValueNumeric(tb.Text))) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) > 3) { tb.Text = "3"; }
            if (Convert.ToInt32(tb.Text) <= 0) { tb.Text = "1"; }
            saveCapacity();
        }

        #endregion region












    }

}

